﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{
    public partial class UpdateVehicles : System.Web.UI.Page
    {
        private static string VIN;
        private static int Car_id;
        private static Car currentCar = new Car();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.IsInRole("Manager"))
            {
                Response.Redirect("~/Denied.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    VIN = Request.QueryString["VIN"].ToString();
                    Car_id = -1;
                    try
                    {
                        DatabaseHandler.connect();
                        currentCar = DatabaseHandler.getCarbyVIN(VIN);
                        Car_id = currentCar.Car_ID;
                        txtyear.Text = currentCar.Year.ToString();
                        txtmake.Text = currentCar.Make;
                        txtmodel.Text = currentCar.Model;
                        ddlType.SelectedIndex = currentCar.VehicleTypeID;
                        txtVIN.Text = currentCar.VIN_Number.ToString();
                        txtExt.Text = currentCar.exColor;
                        txtInt.Text = currentCar.inColor;
                        txtMileage.Text = currentCar.Mileage.ToString();
                        txtEngine.Text = currentCar.EngineType;
                        txttrmn.Text = currentCar.Transmission;
                        txtFuel.Text = currentCar.fuelType;
                        txtDriveType.Text = currentCar.driveType;
                        txthp.Text = currentCar.Horsepower.ToString();
                        txttq.Text = currentCar.Torque.ToString();
                        txtFEcity.Text = currentCar.CityFE.ToString();
                        txtFEhw.Text = currentCar.HighwayFE.ToString();
                        txtMSRP.Text = currentCar.TagPrice.ToString();
                        txtPrice.Text = currentCar.LowestSalePrice.ToString();
                        ddlConditions.SelectedValue = currentCar.Condition;
                        ddlstatus.SelectedIndex = currentCar.Status;
                        txtDate.Text = currentCar.DateReceived.ToShortDateString();
                        txtFeatures.Text = currentCar.Features;
                        txtWarranty.Text = currentCar.Warranty;
                        ddldealer.SelectedIndex = currentCar.D_ID;
                        ((Main)this.Master).clearStatus();

                        lblcarname.Text = currentCar.Year + " " + currentCar.Make + " " + currentCar.Model;

                    }
                    catch (Exception ex)
                    {
                        ((Main)this.Master).writeStatus(ex.Message, true);
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Car savedCar = new Car();
                savedCar.Car_ID = Car_id;
                if (txtyear.Text == string.Empty) savedCar.Year = -1; else savedCar.Year = Convert.ToInt32(txtyear.Text);
                savedCar.Make = txtmake.Text;
                savedCar.Model = txtmodel.Text;
                if (ddlType.SelectedValue == "-1")
                {
                    savedCar.VehicleType = string.Empty;
                    savedCar.VehicleTypeID = -1;
                }
                else
                {
                    savedCar.VehicleType = ddlType.SelectedValue;
                    savedCar.VehicleTypeID = Convert.ToInt32(ddlType.SelectedIndex);
                }
                savedCar.VIN_Number = txtVIN.Text;
                if (txtMileage.Text == string.Empty) savedCar.Mileage = 0; else savedCar.Mileage = Convert.ToInt32(txtMileage.Text);
                savedCar.exColor = txtExt.Text;
                savedCar.inColor = txtInt.Text;
                savedCar.EngineType = txtEngine.Text;
                savedCar.Transmission = txttrmn.Text;
                savedCar.fuelType = txtFuel.Text;
                savedCar.driveType = txtDriveType.Text;
                if (txthp.Text == string.Empty) savedCar.Horsepower = 0; else savedCar.Horsepower = Convert.ToInt32(txthp.Text);
                if (txttq.Text == string.Empty) savedCar.Torque = 0; else savedCar.Torque = Convert.ToInt32(txttq.Text);
                if (txtFEcity.Text == string.Empty) savedCar.CityFE = 0; else savedCar.CityFE = Convert.ToInt32(txtFEcity.Text);
                if (txtFEhw.Text == string.Empty) savedCar.HighwayFE = 0; else savedCar.HighwayFE = Convert.ToInt32(txtFEhw.Text);
                if (txtMSRP.Text == string.Empty) savedCar.TagPrice = 0.00f; else savedCar.TagPrice = (float)(Convert.ToDecimal(txtMSRP.Text));
                if (txtPrice.Text == string.Empty) savedCar.LowestSalePrice = 0.00f; else savedCar.LowestSalePrice = (float)(Convert.ToDecimal(txtPrice.Text));
                if (ddlConditions.SelectedIndex == 0) savedCar.Condition = string.Empty; else savedCar.Condition = ddlConditions.SelectedValue;
                if (ddlstatus.SelectedValue == "-1") savedCar.Status = -1; else savedCar.Status = ddlstatus.SelectedIndex;
                if (txtDate.Text == string.Empty) savedCar.DateReceived = Convert.ToDateTime("1/1/1001"); else savedCar.DateReceived = Convert.ToDateTime(txtDate.Text);
                savedCar.Features = txtFeatures.Text;
                savedCar.Warranty = txtWarranty.Text;
                if (ddldealer.SelectedValue == "-1") savedCar.D_ID = -1; else savedCar.D_ID = ddldealer.SelectedIndex;

                ((Main)this.Master).clearStatus();
                DatabaseHandler.connect();
                DatabaseHandler.updateCar(savedCar);
                ((Main)this.Master).writeStatus("Update complete", false);
            }
            catch (Exception ex)
            {
                ((Main)this.Master).writeStatus(ex.Message, true);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus("You do not have access to remove dealership", true);
            }
            else
            {
                try
                {
                    DatabaseHandler.connect();
                    currentCar = DatabaseHandler.getCarbyVIN(VIN);
                    DatabaseHandler.removeCar(currentCar);
                    ((Main)this.Master).writeStatus("Vehicle Removed", false);
                    btnDelete.Visible = false;
                    btnSave.Visible = false;
                }
                catch (Exception ex)
                {
                    ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
                }
            }
        }
    }
}
